' Copyright (c) 2006, Tom van de Kerkhof (Blog: http://blogs.jpworks.be/Tom)
' All rights reserved.
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions are met:
'
'     * Redistributions of source code must retain the above copyright
'       notice, this list of conditions and the following disclaimer.
'     * Redistributions in binary form must reproduce the above copyright
'       notice, this list of conditions and the following disclaimer in the
'       documentation and/or other materials provided with the distribution.
'     * The name of Tom van de Kerkhof may not be used to endorse or promote products
'       derived from this software without specific prior written permission.
'
' THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
' EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
' DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
' (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
' ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Collections.Generic
Imports ClubStarterKit.Data
Imports SubSonic

Namespace ClubStarterKit.Web
    <Serializable()> _
     Public Class PollQuestion
        Private _pollId As Guid
        Private _question As String
        Private _creationDate As DateTime

        Public Property PollId() As Guid
            Get
                Return _pollId
            End Get
            Set(ByVal Value As Guid)
                _pollId = Value
            End Set
        End Property

        Public Property Question() As String
            Get
                Return _question
            End Get
            Set(ByVal Value As String)
                _question = Value
            End Set
        End Property

        Public Property CreationDate() As DateTime
            Get
                Return _creationDate
            End Get
            Set(ByVal Value As DateTime)
                _creationDate = Value
            End Set
        End Property
    End Class

    Public Class Poll

        Public Shared Function LoadQuestion(ByVal pollId As Guid) As PollQuestion
            If pollId = Guid.Empty Then
                Return Nothing
            End If

            Dim quest As New PollQuestions(PollQuestions.Columns.PollId, pollId)
            Dim q As PollQuestion = New PollQuestion()
            q.PollId = pollId
            q.Question = quest.Question
            q.CreationDate = quest.CreationDate
            Return q
        End Function

        Public Shared Function GetLatestQuestion() As PollQuestion

            Dim qry As New Query(Tables.PollQuestions)
            qry.Top = "1"
            qry.OrderBy = OrderBy.Desc(PollQuestions.Columns.CreationDate)
            qry.SelectList = PollQuestions.Columns.PollId & "," & PollQuestions.Columns.Question & "," & PollQuestions.Columns.CreationDate

            Dim rdr As IDataReader = qry.ExecuteReader()
            Dim q As New PollQuestion
            While rdr.Read()
                q.PollId = rdr.GetGuid(0)
                q.Question = rdr.GetString(1)
                q.CreationDate = rdr.GetDateTime(2)
            End While
            rdr.Close()
            Return q
        End Function

        Public Shared Function LoadQuestions() As PollQuestionsCollection
            Dim rdr As IDataReader = PollQuestions.FetchAll()
            Dim col As New PollQuestionsCollection
            col.LoadAndCloseReader(rdr)
            Return col
        End Function

        Public Shared Function CheckUserAlreadyVoted(ByVal pollId As Guid, ByVal userId As Guid) As Boolean
            Dim qry As New Query(Tables.PollVotes)
            qry.AddWhere(PollVotes.Columns.PollId, pollId)
            qry.AddWhere(PollVotes.Columns.UserId, userId)
            If qry.GetRecordCount() > 0 Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Shared Function CountTotalVotes(ByVal pollId As Guid) As Integer
            Dim qry As New Query(Tables.PollVotes)
            qry.AddWhere(PollVotes.Columns.PollId, pollId)
            Return qry.GetRecordCount()
        End Function

        Public Shared Function CountTotalReactions(ByVal pollId As Guid) As Integer
            Dim qry As New Query(Tables.PollReactions)
            qry.AddWhere(PollReactions.Columns.PollId, pollId)
            Return qry.GetRecordCount()
        End Function

        Public Shared Sub Vote(ByVal pollId As Guid, ByVal answerId As Guid, ByVal userId As Guid)
            PollVotes.Insert(Guid.NewGuid(), userId, answerId, DateTime.Now, pollId)
        End Sub

        Public Shared Sub AddPollReaction(ByVal reaction As String, ByVal pollId As Guid, ByVal userId As Guid)
            PollReactions.Insert(Guid.NewGuid(), userId, DateTime.Now, reaction, pollId)
        End Sub

        Public Shared Sub DeleteReaction(ByVal reactionId As Guid)
            PollReactions.Delete(PollReactions.Columns.ReactionId, reactionId)
        End Sub

        Public Shared Function GetAnswersForPollQuestion(ByVal pollQuestionId As Guid) As PollAnswersCollection
            If pollQuestionId = Guid.Empty Then
                Return Nothing
            End If

            Dim rdr As IDataReader = PollAnswers.FetchByParameter(PollAnswers.Columns.QuestionId, pollQuestionId)
            Dim col As New PollAnswersCollection
            col.LoadAndCloseReader(rdr)
            Return col
        End Function

        Public Shared Function GetReactionsForPollQuestion(ByVal pollQuestionId As Guid) As PollReactionsCollection
            If pollQuestionId = Guid.Empty Then
                Return Nothing
            End If

            Dim rdr As IDataReader = PollReactions.FetchByParameter(PollReactions.Columns.PollId, pollQuestionId)
            Dim col As New PollReactionsCollection
            col.LoadAndCloseReader(rdr)
            Return col
        End Function

        Public Shared Function NumberOfVotesByAnswer(ByVal answerid As System.Guid) As Integer
            Dim qry As New Query(Tables.PollVotes)
            qry.AddWhere(PollVotes.Columns.PollAnswerId, answerid)
            Return qry.GetRecordCount()
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function SelectAllQuestions() As IDataReader
            Return ClubStarterKit.Data.PollQuestions.FetchAll()
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Insert, False)> _
        Public Shared Sub InsertQuestion(ByVal PollId As System.Guid, ByVal Question As String, ByVal CreationDate As Date, ByVal Memberid As System.Guid)
            ClubStarterKit.Data.PollQuestions.Insert(PollId, Question, CreationDate, Memberid)
        End Sub

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Update, False)> _
        Public Shared Sub UpdateQuestion(ByVal PollId As System.Guid, ByVal Question As String, ByVal CreationDate As Date, ByVal Memberid As System.Guid)
            ClubStarterKit.Data.PollQuestions.Update(PollId, Question, CreationDate, Memberid)
        End Sub

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Delete, False)> _
        Public Shared Sub DeleteQuestion(ByVal PollId As System.Guid)
            ClubStarterKit.Data.PollQuestions.Delete(PollQuestions.Columns.PollId, PollId)
        End Sub
    End Class
End Namespace